package sketch.specs;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;

import sketch.ast.ASTUtils;
import sketch.util.Checker;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class SourceExpansionTest extends TestCase {
	
	public static Test suite() {
        return new TestSuite(SourceExpansionTest.class);
    }
	
	public void testBlockReplacementChooseOne() {
		System.out.println("Executing testBlockReplacementChooseOne...");
//		String filePath = "./tests/sketch/specs/samplecode.txt";;
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		Map<Comment, SketchedBlock> sketchedBlocks = creator.getSketchedBlocks();
		System.out.println(sketchedBlocks.size());
		assertTrue(sketchedBlocks.size() == 10);
		
		Collection<SketchedBlock> blockList = sketchedBlocks.values();
		for(SketchedBlock block : blockList) {
			if(block instanceof SketchedChooseOneBlock) {
				System.out.println(" -------- processing choose one block -------- ");
				System.out.println("original block: ");
				System.out.println(block.getRealBlock());
				//System.out.println("parent block: \n" + block.realBlock.getParent());
				
				Checker.checkTrue(block.getRealBlock().getParent() instanceof Block,
						"The parent type is: " + block.getRealBlock().getParent().getClass());
				Block parentBlock = (Block)block.getRealBlock().getParent();
				
				System.out.println(" -+-+-+-+-+ ");
				List<Block> replicated = block.replicate();
				for(Block b : replicated) {
					System.out.println(b);
					System.out.println("let's try to replace that, here is the result:");
					SourceExpansionVisitor expansionVisitor = new SourceExpansionVisitor(parentBlock, block.getRealBlock(), b);
					Block afterReplace = expansionVisitor.getReplacedBlock();
					System.out.println(afterReplace);
					System.out.println("++++++");
				}
				System.out.println("\n");
			}
		}
		System.out.println("\n\n\n");
	}
	
	public void testBlockReplacementChooseAll() {
		System.out.println("Executing testBlockReplacementChooseAll...");
//		String filePath = "./tests/sketch/specs/samplecode.txt";;
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		Map<Comment, SketchedBlock> sketchedBlocks = creator.getSketchedBlocks();
		System.out.println(sketchedBlocks.size());
		assertTrue(sketchedBlocks.size() == 10);
		
		Collection<SketchedBlock> blockList = sketchedBlocks.values();
		for(SketchedBlock block : blockList) {
			if(block instanceof SketchedChooseAllBlock) { //the only different
				System.out.println(" -------- processing choose one block -------- ");
				System.out.println("original block: ");
				System.out.println(block.getRealBlock());
				//System.out.println("parent block: \n" + block.realBlock.getParent());
				
				Checker.checkTrue(block.getRealBlock().getParent() instanceof Block,
						"The parent type is: " + block.getRealBlock().getParent().getClass());
				Block parentBlock = (Block)block.getRealBlock().getParent();
				
				System.out.println(" -+-+-+-+-+ ");
				List<Block> replicated = block.replicate();
				for(Block b : replicated) {
					System.out.println(b);
					System.out.println("let's try to replace that, here is the result:");
					SourceExpansionVisitor expansionVisitor = new SourceExpansionVisitor(parentBlock, block.getRealBlock(), b);
					Block afterReplace = expansionVisitor.getReplacedBlock();
					System.out.println(afterReplace);
					System.out.println("++++++");
				}
				System.out.println("\n");
			}
		}
		System.out.println("\n\n\n");
	}
}
